package org.hive.testserver.domain.model;

import java.util.Collections;
import java.util.Map;

import org.apache.commons.collections.ComparatorUtils;

/**
 * Summary snapshot of a test run's slave for use by the web server.
 */
public class WorkSummary implements Comparable<WorkSummary> {

	private String slaveName;
	private String workId;
	private String workType;
	private String workTestName;
	private long workStarted;
	private String workResult;
	private Map<String,String> workContext;
	private String workScheduleReason;

	public WorkSummary(Work work) {
		this(work.assignedSlave(), work);
	}
	
	@SuppressWarnings("unchecked")
	public WorkSummary(Slave slave, Work work) {
		super();
		slaveName = slave.getName();
		workId = work != null ? work.getId() : "";
		workType = work != null ? work.getClass().getSimpleName() : "";
		workTestName = work != null ? work.getTestName() : "";
		workStarted = work != null ? work.started() : -1;
		workResult = work != null ? work.result() : "";
		workContext = work != null ?  work.resultContext() : Collections.EMPTY_MAP;
		workScheduleReason = work != null ? work.scheduledReason() : "";
	}

	@SuppressWarnings("unchecked")
	public int compareTo(WorkSummary o) {
		return ComparatorUtils.NATURAL_COMPARATOR.compare(this.slaveName, o.slaveName);
	}

	public String getSlaveName() {
		return slaveName;
	}

	public String getWorkId() {
		return workId;
	}

	public String getWorkType() {
		return workType;
	}

	public String getWorkTestName() {
		return workTestName;
	}

	public long getWorkStarted() {
		return workStarted;
	}

	public String getWorkResult() {
		return workResult;
	}

	public String getScheduleReason() {
		return workScheduleReason;
	}

	public Map<String,String> getWorkResultContext() {
		return Collections.unmodifiableMap(workContext);
	}
}
